<template>  
    <view class="container">  
		
		<view class="user-section">
			<image class="bg" src="/static/user-bg.jpg"></image>
		</view>
		
		<view 
			class="cover-container"
			:style="[{
				transform: coverTransform,
				transition: coverTransition
			}]"
		>
			<image class="arc" src="/static/arc.png"></image>
			<view class="tj-sction">
				<view class="tj-item" @click="navTo('/pages/set/set')">
					<view class="user-info-box uni-flex uni-column">
						<view class="portrait-box flex-item flex-item-V ">
							<image v-if="userInfo.headimg" class="portrait" :src="this.$imghost + userInfo.headimg"></image>
							<image v-if="!userInfo.headimg" class="portrait" src="/static/missing-face.png" ></image>
						</view>
						<view class="info-box flex-item flex-item-V ">
							<text class="username">{{userInfo.name || this.$lang.user_user.default_name}}</text>
						</view>
					</view>
				</view>
			</view>
			<!-- 浏览历史 -->
			<view class="history-section icon">
				<list-cell iconColor="#e07472" :title="this.$lang.user_user.my_coupons" :number="my_coupon_count" :numberType="'primary'" :numberInverted="false" @eventClick="navTo('/pages/order/order?state=0&total=' + my_coupon_count)"></list-cell>
				<list-cell iconColor="#9789f7" :title="this.$lang.user_user.share_title" @eventClick="share"></list-cell>
				<list-cell iconColor="#ee883b" :title="this.$lang.user_user.my_messages" :number="no_readed_num" :numberType="'error'" @eventClick="navTo('/pages/notice/notice', true)"></list-cell>
				<list-cell iconColor="#ee883b" :title="this.$lang.user_user.about_us" @eventClick="navTo('/pages/user/about')"></list-cell>
				<list-cell iconColor="#ee883b" :title="this.$lang.user_user.check_title" :tips="this.$lang.user_user.version_title+' '+this.upgrade.infVersion+'.'+this.upgrade.infVersionCode" @eventClick="aupgrade"></list-cell>
				<list-cell iconColor="#e07472" :title="this.$lang.user_user.set_title" border="" @eventClick="navTo('/pages/set/set')"></list-cell>
				<!-- <list-cell icon="icon-shezhi1" iconColor="#e07472" title="test" border="" @eventClick="navTo2('/pages/user/test')"></list-cell> -->
			</view>
		</view>
		<luna-upgrade ref="showtip" :nowversion="nowversion" :newversion="newversion" :url="upgrade.url" :notes="upgrade.notes" :lang="lang"></luna-upgrade>
    </view>  
</template>  
<script>  
	import listCell from '@/components/mix-list-cell';
	import LunaUpgrade from '@/components/luna-upgrade/luna-upgrade.vue';
	import { setTitleNViewStyle } from '@/common/common';
	import { inArray } from '@/common/common';
    import {  
        mapState 
    } from 'vuex';  
	let startY = 0, moveY = 0, pageAtTop = true;
    export default {
		components: {
			listCell,
			LunaUpgrade
		},
		data(){
			return {
				coverTransform: 'translateY(0px)',
				coverTransition: '0s',
				moving: false,
				my_coupon_count: 0,
				no_readed_num:0,
				newversion: '',
				nowversion: '',
				lang: {},
			}
		},
		onShow() {
			
			//设置右上角按钮是否有红点
			this.setNoticeReddot();
			uni.setNavigationBarTitle({
			    title: this.$lang.user_user.title
			});
			uni.setTabBarItem({
				text: this.$lang.tabbar.mine_text
			})
			this.getMyConponNumber();
			this.lang = this.$lang.upgrade;
		},
		onLoad(){
			// setTitleNViewStyle(0, this.$store.state.newNotice);
			this.getMyConponNumber();
		},
		// #ifndef MP
		onNavigationBarButtonTap(e) {
			const index = e.index;
			console.log(index)
			if (index === 0) {
				this.navTo('/pages/set/set');
			}else if(index === 1){
				// #ifdef APP-PLUS
				const pages = getCurrentPages();
				const page = pages[pages.length - 1];
				const currentWebview = page.$getAppWebview();
				currentWebview.hideTitleNViewButtonRedDot({
					index: index
				});
				// #endif
				uni.navigateTo({
					url: '/pages/notice/notice'
				})
			}
		},
		// #endif
        computed: {
			...mapState(['hasLogin','userInfo', 'upgrade']),
			i18n() {
				return this.$lang.user_user;
			}
		},
		watch: {
			hasLogin(newValue) {
				if (newValue) {
					this.getMyConponNumber();
				}
			}
		},
        methods: {
			aupgrade(){
				//#ifndef APP-PLUS
				return false;
				//#endif 
				//#ifdef APP-PLUS
				var _that = this;
				plus.runtime.getProperty(plus.runtime.appid,function(inf){
					console.log('set upgrade', _that.upgrade, inf.version, inf.versionCode);
					if(_that.upgrade.status == 1){
						if(_that.upgrade.version != inf.version || _that.upgrade.versionCode != inf.versionCode){
							if(!_that.upgrade.wgtUrl){
								_that.$nextTick(() => {
									_that.$refs['showtip'].open();
									_that.notes = _that.upgrade.notes;
									_that.url = _that.upgrade.url;
									_that.nowversion = inf.version + '.' + inf.versionCode;
									_that.newversion = _that.upgrade.version + '.' + _that.upgrade.versionCode;
									_that.only_close = true;
								});
							}
						}
					}
				});
				//#endif 
			},
			async setNoticeReddot(){
				var readedIds_key = 'pages/notice/notice_readed_ids';
				
				try {
				    var readedIds_value = uni.getStorageSync(readedIds_key);
					console.log('user onload '+readedIds_key, readedIds_value)
				} catch (e) {
				    // error
					console.log(e)
					return false;
				}
				if(!readedIds_value){
					readedIds_value = [];
				}
				var isAllReddot = false;
				var noreadedNum = 0;
				var types = [0,1,2];
				for(var index = 0; index < types.length; index++){
					var res = await this.$request_async({
						name: 'notice.list',
						method: 'POST',
						data: {page : 1, type : types[index]}
					})
					console.log(res);
					if(res.code != 0){
						return false;
					}
					var pageInfo = res.data;
					var oneRed = false;
					pageInfo.data.forEach(item=>{
						
						if(!item.readed && !inArray(item.id, readedIds_value)){
							oneRed = true;
							noreadedNum++;
						}
						
					});
					if(oneRed){
						isAllReddot = true;
					}
				}
				console.log('isAllReddot', isAllReddot);
				console.log('noreadedNum', noreadedNum);
				// setTitleNViewStyle(1, isAllReddot);
				this.$store.state.newNotice = isAllReddot;
				this.no_readed_num = noreadedNum;
			},
			getMyConponNumber() {
				var that = this;
				if (!this.hasLogin) return false;
				this.$request({name: 'product.my_coupon_number', success: function(res) {
					console.log(res);
					if (res.data.code == 0) {
						that.my_coupon_count = res.data.data
					}
				}});
			},
			share(){
				this.$api.msg('需要分享页');
			},
			/**
			 * 统一跳转接口,拦截未登录路由
			 * navigator标签现在默认没有转场动画，所以用view
			 */
			navTo(url, toBar = false){
				if(!this.hasLogin){
					url = '/pages/public/login';
				}
				if (toBar) {
					console.log('switch')
					uni.switchTab({
						url: url,
						error(error) {
							console.log(error)
						}
					})
				} else {
					uni.navigateTo({  
						url
					})  
				}
			}, 
			navTo2(url){
				uni.navigateTo({  
					url
				})
			}, 
			toRegist() {
				uni.navigateTo({
					url: `/pages/public/register`
				})
			},
			toLogin() {
				uni.navigateTo({
					url: `/pages/public/login`
				})
			},
			/**
			 *  会员卡下拉和回弹
			 *  1.关闭bounce避免ios端下拉冲突
			 *  2.由于touchmove事件的缺陷（以前做小程序就遇到，比如20跳到40，h5反而好很多），下拉的时候会有掉帧的感觉
			 *    transition设置0.1秒延迟，让css来过渡这段空窗期
			 *  3.回弹效果可修改曲线值来调整效果，推荐一个好用的bezier生成工具 http://cubic-bezier.com/
			 */
			coverTouchstart(e){
				if(pageAtTop === false){
					return;
				}
				this.coverTransition = 'transform .1s linear';
				startY = e.touches[0].clientY;
			},
			coverTouchmove(e){
				moveY = e.touches[0].clientY;
				let moveDistance = moveY - startY;
				if(moveDistance < 0){
					this.moving = false;
					return;
				}
				this.moving = true;
				if(moveDistance >= 80 && moveDistance < 100){
					moveDistance = 80;
				}
		
				if(moveDistance > 0 && moveDistance <= 80){
					this.coverTransform = `translateY(${moveDistance}px)`;
				}
			},
			coverTouchend(){
				if(this.moving === false){
					return;
				}
				this.moving = false;
				this.coverTransition = 'transform 0.3s cubic-bezier(.21,1.93,.53,.64)';
				this.coverTransform = 'translateY(0px)';
			}
        }  
    }  
</script>  
<style lang='scss'>
	%flex-center {
	 display:flex;
	 flex-direction: column;
	 justify-content: center;
	 align-items: center;
	}
	%section {
	  display:flex;
	  justify-content: space-around;
	  align-content: center;
	  background: #fff;
	  border-radius: 10upx;
	}

	.user-section{
		height: 330upx;
		padding: 100upx 30upx 0;
		position:relative;
		.bg{
			position:absolute;
			left: 0;
			top: 0;
			width: 100%;
			height: 100%;
			filter: blur(1px);
			opacity: .7;
		}
	}
	.user-info-box{
		height: 180upx;
		display:flex;
		align-items:center;
		position:relative;
		z-index: 1;
		.portrait{
			width: 130upx;
			height: 130upx;
			border:5upx solid #fff;
			border-radius: 50%;
		}
		.username{
			font-size: $font-lg + 6upx;
			color: $font-color-dark;
			margin-left: 20upx;
		}
	}

	.vip-card-box{
		display:flex;
		flex-direction: column;
		color: #f7d680;
		height: 240upx;
		background: linear-gradient(left, rgba(0,0,0,.7), rgba(0,0,0,.8));
		border-radius: 16upx 16upx 0 0;
		overflow: hidden;
		position: relative;
		padding: 20upx 24upx;
		.card-bg{
			position:absolute;
			top: 20upx;
			right: 0;
			width: 380upx;
			height: 260upx;
		}
		.b-btn{
			position: absolute;
			right: 20upx;
			top: 16upx;
			width: 132upx;
			height: 40upx;
			text-align: center;
			line-height: 40upx;
			font-size: 22upx;
			color: #36343c;
			border-radius: 20px;
			background: linear-gradient(left, #f9e6af, #ffd465);
			z-index: 1;
		}
		.tit{
			font-size: $font-base+2upx;
			color: #f7d680;
			margin-bottom: 28upx;
			.yticon{
				color: #f6e5a3;
				margin-right: 16upx;
			}
		}
		.e-b{
			font-size: $font-sm;
			color: #d8cba9;
			margin-top: 10upx;
		}
	}
	.cover-container{
		background: $page-color-base;
		margin-top: -150upx;
		padding: 0 30upx;
		position:relative;
		background: #f5f5f5;
		padding-bottom: 20upx;
		.arc{
			position:absolute;
			left: 0;
			top: -34upx;
			width: 100%;
			height: 36upx;
		}
	}
	.tj-sction{
		@extend %section;
		.tj-item{
			@extend %flex-center;
			flex-direction: column;
			height: 230upx;
			font-size: $font-sm;
			color: #75787d;
		}
		.num{
			font-size: $font-lg;
			color: $font-color-dark;
			margin-bottom: 8upx;
		}
	}
	.order-section{
		@extend %section;
		padding: 28upx 0;
		margin-top: 20upx;
		.order-item{
			@extend %flex-center;
			width: 120upx;
			height: 120upx;
			border-radius: 10upx;
			font-size: $font-sm;
			color: $font-color-dark;
		}
		.yticon{
			font-size: 48upx;
			margin-bottom: 18upx;
			color: #fa436a;
		}
		.icon-shouhoutuikuan{
			font-size:44upx;
		}
	}
	.history-section{
		padding: 30upx 0 0;
		margin-top: 20upx;
		background: #fff;
		border-radius:10upx;
		.sec-header{
			display:flex;
			align-items: center;
			font-size: $font-base;
			color: $font-color-dark;
			line-height: 40upx;
			margin-left: 30upx;
			.yticon{
				font-size: 44upx;
				color: #5eba8f;
				margin-right: 16upx;
				line-height: 40upx;
			}
		}
		.h-list{
			white-space: nowrap;
			padding: 30upx 30upx 0;
			image{
				display:inline-block;
				width: 160upx;
				height: 160upx;
				margin-right: 20upx;
				border-radius: 10upx;
			}
		}
	}
	
</style>